from selenium import webdriver
import time
import datetime
from selenium.webdriver.support.wait import WebDriverWait


# 标志位
flag = 0

# 北京时间2020-xx-xx 10:07:20 准时运行
targetDate = datetime.datetime(2020, 12, 27, 10, 7, 0)
# targetDate = datetime.datetime(2020, 12, 24, 23, 38, 20)


def wait_for_time(flag):
    now = datetime.datetime.now()
    # 到达设定时间，结束循环
    if targetDate > now:
        print("时间没到: " + str(now))
        return 0
    else:
        print("时间到了: " + str(targetDate))
        print("程序启动！")
        return 1


while flag == 0:
    flag = wait_for_time(flag)
    time.sleep(2)

# 重置标志位
flag = 0

driver = webdriver.Chrome(r'C:\software\ariya\install\chromedriver_win32-87.0.4280.88\chromedriver.exe')

# 先打开一次,放入cookie，字典格式
driver.get('https://www.vmall.com/product/10086726905036.html?ANONYMITY_LOGIN_NAME=177****4521#10086963883857')#带手机号的手机页面
cookies = [{'domain': 'www.vmall.com', 'httpOnly': False}]
for item in cookies:
    driver.add_cookie(item)

# 打开活动页面
driver.get('https://www.vmall.com/product/10086726905036.html?ANONYMITY_LOGIN_NAME=177****4521#10086963883857')#带手机号的手机页面
# 留40s去登陆
# time.sleep(40)
# 拿到cookie
# cookie = driver.get_cookies()
# print(cookie)
print('登录完成')


# 创建订单函数
def create_bill(flag):
    try:
        # 等一秒,显式等待网页立即购买的按钮刷出来,抛异常被捕获，然后刷新继续等,每0.01秒检测一次,如果超过1秒还没有,就刷新
        WebDriverWait(driver, 1, 0.01).until(
            lambda el: driver.find_element_by_xpath(
                '//span[contains(text(),"立即申购")]'))  # //*[@id="pro-operation"][@title="立即下单"]

        # 等釉白色按钮模块和内存选项出来
        WebDriverWait(driver, 1, 0.01).until(
            lambda el: driver.find_element_by_xpath(
                '//*[@id="pro-skus"]'))

        WebDriverWait(driver, 1, 0.01).until(
            lambda el: driver.find_element_by_xpath(
                '//span[contains(text(),"5G全网通 8GB+128GB")]'))

        # 捕获内存按钮
        search_button_memory = driver.find_element_by_xpath(
            '//span[contains(text(),"立即申购")]')  # //*[@id="ljgm"][@title="立即下单"]
        # 选择内存
        search_button_memory.click()

        # 捕获下单按钮
        search_button_submit = driver.find_element_by_xpath(
            '//span[contains(text(),"立即申购")]')  # //*[@id="ljgm"][@title="立即下单"]
        # 下单
        search_button_submit.click()

        flag = 1
    except Exception:
        print('时间没到')
        # driver.refresh()
        driver.get(
            'https://www.vmall.com/product/10086726905036.html?ANONYMITY_LOGIN_NAME=177****4521#10086963883857')  # 带手机号的手机页面
    return flag


while flag == 0:
    # 不等于0说明已经进入订单界面
    flag = create_bill(flag)


# 重置标志位
flag = 0


def submit_bill(flag):
    try:
        # TODO 等待，等待提交订单按钮和地址加载出来
        WebDriverWait(driver, 120, 0.01).until(
            lambda el: driver.find_element_by_xpath('//span[contains(text(),"提交订单")]'))
        # 等快递地址加载完毕
        WebDriverWait(driver, 120, 0.01).until(
            lambda el: driver.find_element_by_xpath('//div[@class="address-detail"]'))

        submit_btn = driver.find_element_by_xpath('//span[contains(text(),"提交订单")]')
        submit_btn.click()
        flag = 1
        print('抢购成功')
    except Exception:
        print('提交按钮还没出来')
        # driver.refresh()
        # driver.get(
        #     'https://www.vmall.com/product/10086726905036.html?ANONYMITY_LOGIN_NAME=177****4521#10086963883857')  # 带手机号的手机页面
    return flag


while flag == 0:
    # 不等于0说明已经提交了
    flag = submit_bill(flag)


# 关闭浏览器
time.sleep(600)
driver.quit()
